// pages/poster/position/position.js
import * as zrender from '../zrender/zrender';
import * as zrhelper from '../zrender/zrender-helper';
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = ''
let zr;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1, // 公司id
    reAvatorTem: '',
    companyTem: '',
    miniCodeTemp: '',
    companyCover: '',
    companyName: '',
    companyStaffNum: -1,
    companyIndustry: '',
    reName: '',
    recommendClassifyId: 0,
    rewardId: 0,
    url: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { id, reAvator, reName, position, salary, work, edu, recommendClassifyId, rewardId, url } = options;
    this.data.id = id;
    this.data.reAvator = reAvator;
    this.data.reName = reName;
    this.data.position = position;
    this.data.salary = salary;
    this.data.work = work;
    this.data.edu = edu;
    this.data.recommendClassifyId = recommendClassifyId;
    this.data.rewardId = rewardId;
    this.data.url = url;

    token = getApp().globalData.token;
    this.getDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  // 公司详情
  getDetail() {
    T.req({
      loadTitle: '初始化中...',
      url: config.api + 'company/list/' + this.data.id,
      method: 'GET',
      data: {
        token: token,
      },
      success: (res) => {
        this.data.companyCover = res.cover;
        this.data.companyName = res.company;
        this.data.companyStaffNum = res.staffNumber;
        this.data.companyIndustry = res.industryName;
        this.getReommendAvator();
      }
    })
  },

  /**
  * 推荐人头像
  */
  getReommendAvator() {
    wx.showLoading({
      title: '生成中,请稍候',
    });
    new Promise((resolve, rejet) => {
      wx.getImageInfo({
        src: this.data.reAvator,
        success: (res) => {
          resolve(res);
        }
      })
    }).then((res) => {
      this.data.reAvatorTem = res.path;
      this.getCompanyCover();
    })
  },

  getCompanyCover() {
    new Promise((resolve, rejet) => {
      wx.getImageInfo({
        src: this.data.companyCover,
        success: (res) => {
          resolve(res);
        }
      })
    }).then((res) => {
      this.data.companyTem = res.path;
      this.getMinicode();
    })
  },

  /**
  * 小程序码-应聘端的
  */
  getMinicode() {
    let param = {
      scene: 'id=' + this.data.rewardId + '&recommendClassifyId=' + recommendClassifyId + '&isShare=true',
      page: this.data.url,
      width: 127
    }
    wx.downloadFile({
      url: config.host + 'employee/mini_code?' + T.param(param),
      success: (res) => {
        wx.getImageInfo({
          src: res.tempFilePath,
          success: (_res) => {
            console.log(_res.path)
            this.data.miniCodeTemp = _res.path;
            // 绘画
            this.renderCanvas();
          },
          fail: (err) => {
            wx.hideLoading();
            wx.showToast({
              title: err.errMsg,
              icon: 'none'
            })
          }
        })
      }
    })
  },

  zrAddImage(x, y, url, w, h) {
    zr.add(new zrender.Image({
      style: {
        x: x,
        y: y,
        image: url,
        width: w,
        height: h
      }
    }));
  },

  zrAddText(x, y, text, textFont, textAlign) {
    zr.add(new zrender.Text({
      style: {
        x: x,
        y: y,
        text: text,
        textFill: '#fff',
        textFont: textFont || '12px Microsoft Yahei',
        textAlign: textAlign || 'left'
      }
    }));
  },

  zrAddCircleImg(x, y, url, w, h, cx, cy, r) {
    let recomAvator = new zrender.Image({
      style: {
        x: x,
        y: y,
        image: url,
        width: w,
        height: h
      }
    });
    let circle = new zrender.Circle({
      shape: {
        cx: cx,
        cy: cy,
        r: r
      }
    });
    recomAvator.setClipPath(circle);
    zr.add(recomAvator);
  },

  zrAddLine(x1, y1, x2, y2) {
    zr.add(new zrender.Line({
      shape: {
        x1: x1,
        y1: y1,
        x2: x2,
        y2: y2
      },
      style: {
        lineWidth: 2,
        stroke: '#fff',
      }
    }));
  },

  renderCanvas() {
    zr = zrhelper.createZrender('canvas');
    // 上部背景图
    this.zrAddImage(0, 0, '../poster_orange_bg.png', 375, 480);
    // 推荐用户-头像
    this.zrAddCircleImg(79, 40, this.data.reAvatorTem, 35, 35, 96.5, 57.5, 17.5);
    // 文案说明-顶部
    this.zrAddText(121, 41, this.data.reName + '向您推荐了一份简历,')
    this.zrAddText(121, 58, '赶快扫描下方小程序码看一看吧~')

    // 被推荐职位的公司封面
    this.zrAddImage(148, 115, this.data.companyTem, 80, 80);

    // 被推荐公司的名
    this.zrAddText(187.5, 220, this.data.companyName, '20px Microsoft Yahei', 'center');

    // 公司规模
    this.zrAddText(175, 253, this.data.companyStaffNum, null, 'right');
    this.zrAddLine(185, 251, 185, 265);
    // 所属行业
    this.zrAddText(195, 253, this.data.companyIndustry, null, 'left');

    // 推荐职位名
    this.zrAddText(187.5, 296, this.data.position, '20px Microsoft Yahei', 'center');

    // 薪资范围
    this.zrAddText(187.5, 334, this.data.salary, '24px Microsoft Yahei', 'center');

    // 工作经验
    this.zrAddText(175, 367, this.data.work, null, 'right');
    this.zrAddLine(185, 365, 185, 379);
    // 学历
    this.zrAddText(195, 367, this.data.edu, null, 'left');

    let rect = new zrender.Rect({
      style: {
        fill: '#fff',
      },
      shape: {
        x: 0,
        y: 480,
        width: 375,
        height: 187
      }
    });
    zr.add(rect);
    // 小程序码
    this.zrAddImage(124, 502, this.data.miniCodeTemp, 127, 127);

    zr.on('rendered', () => {
      wx.hideLoading();
      setTimeout(() => {
        wx.canvasToTempFilePath({
          canvasId: 'canvas',
          quality: 1,
          fileType: 'jpg',
          success: (res) => {
            this.setData({
              postImg: res.tempFilePath
            });
          }
        })
      }, 500);
    })
  },

  onKeepPoster() {
    wx.showLoading({
      title: '保存中...',
    })
    setTimeout(() => {
      wx.canvasToTempFilePath({
        canvasId: 'canvas',
        quality: 1,
        fileType: 'jpg',
        success: (res) => {
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: () => {
              wx.hideLoading();
              T.toast({ id: '#toast', text: "已经保存到手机照片" });
            },
            fail: (err) => {
              console.log(err);
            }
          })
        }
      })
    }, 0.5 * 1000);
  }
})